package com.example.pest.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.pest.mapper.XxRoomReservationMapper;
import com.example.pest.pojo.XxRoomReservation;
import com.example.pest.pojo.XxUserReservation;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author aisen
* @description 针对表【xx_room_reservation(自习室预约表)】的数据库操作Service实现
* @createDate 2024-12-20 00:14:58
*/
@Service
public class XxRoomReservationServiceImpl extends ServiceImpl<XxRoomReservationMapper, XxRoomReservation> {

    @Resource
    XxRoomReservationMapper xxRoomReservationMapper;

    @Resource
    XxUserReservationServiceImpl userReservationService;

    //预约
    @Transactional(rollbackFor = Exception.class)
    public boolean yuYue(long userId, long roomId,long id) {
        XxRoomReservation reservation = getById(id);
        if (reservation == null ) {
            return false;
        }
        if(userReservationService.lambdaQuery().eq(XxUserReservation::getRid,id).eq(XxUserReservation::getUserId,userId).count() > 0){
            return false;
        }
        if (xxRoomReservationMapper.qiangWeiZhi(id) > 0){
            XxUserReservation userReservation = new XxUserReservation();
            userReservation.setUserId(userId);
            userReservation.setRoomId(roomId);
            userReservation.setDateKey(reservation.getDateKey());
            userReservation.setTimeSpan(reservation.getTimeSpan());
            userReservation.setRid(id);
            userReservation.setCreateTime(System.currentTimeMillis());
            return userReservationService.save(userReservation);
        }
        return false;

    }
}




